package com.qlslylq.ad.sdk.rx.subscribers;

import com.google.gson.JsonSyntaxException;
import com.qlslylq.ad.sdk.constant.AppConst;
import com.qlslylq.ad.sdk.enums.ErrorEnum;
import com.qlslylq.ad.sdk.model.rsp.BooleanVO;
import com.qlslylq.ad.sdk.model.rsp.ErrorResponse;
import com.qlslylq.ad.sdk.net.ResponseBase;
import com.qlslylq.ad.sdk.net.ResponseObject;
import com.qlslylq.ad.sdk.util.Log;

import org.apache.http.conn.ConnectTimeoutException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.subscribers.ResourceSubscriber;

/**
 * created by qlslylq <br/>
 * on 2022/1/12 <br/>
 * 没有进度条的订阅者,后台加载,并在页面显示<br/>
 */
public class QuietSubscriber<T> extends ResourceSubscriber<T> {

    private OnNext nextListener;

    public QuietSubscriber(OnNext nextListener) {
        this.nextListener = nextListener;
    }

    @Override
    protected void onStart() {
        request(1);
    }

    @Override
    public void onNext(T t) {
        ResponseBase rsp = (ResponseBase) t;
        if (rsp == null) {
            onError(new Exception("响应模型不匹配"));
            return;
        }

        //数据不合法
        if (ErrorEnum.isDataInvalid(rsp.getCode())) {
            ResponseObject jo = new ResponseObject();
            jo.setCode(rsp.getCode());
            jo.setError(rsp.getError());
            jo.setMessage(rsp.getMessage());

            errorListener(jo);
            return;
        }

        //数据合法
        if (needPrint(t)) {
            Log.dpj(rsp);
        }
        nextListener.onNext(t);
    }

    /**
     * 私密日志[支线]过滤<br/>
     * 仅过滤onNext<br/>
     * 用于过滤接口：埋点<br/>
     */
    private boolean needPrint(T t) {
        if (!AppConst.openSecretLog) {
            return false;
        }
        if (!AppConst.openSecretBranchLog) {
            if (t instanceof ResponseObject) {
                ResponseObject ro = (ResponseObject) t;
                if (ro.getData() != null && ro.getData().getClass() == BooleanVO.class) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 包装特定网络异常到响应结果中,方便从响应中看出错误<br/>
     */
    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        ResponseObject jo;
        if (e instanceof JsonSyntaxException) {
            jo = ErrorResponse.errorJsonDataResponse();
        } else if (e instanceof SocketTimeoutException) {
            jo = ErrorResponse.errorSocketTimeoutResponse();
        } else if (e instanceof ConnectTimeoutException) {
            jo = ErrorResponse.errorConnectTimeoutResponse();
        } else if (e instanceof ConnectException) {
            jo = ErrorResponse.errorConnectResponse();
        } else {
            jo = ErrorResponse.errorNetOtherResponse();
        }
        jo.setError(e + "");

        errorListener(jo);
    }

    @Override
    public void onComplete() {

    }

    /**
     * 错误回调
     */
    private void errorListener(ResponseObject jo) {
        if (nextListener instanceof OnNextOnError) {
            Log.dpj(jo);
            ((OnNextOnError) nextListener).onError(jo);
        }
    }
}